package com.azkj.album.view.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.azkj.album.R;
import com.azkj.album.dto.GalleryBean;
import com.azkj.album.dto.MessageEvent;
import com.azkj.album.dto.MessageType;
import com.azkj.album.dto.UploadBean;
import com.azkj.album.network.NetworkMaster;
import com.azkj.album.network.callback.UploadCallback;
import com.azkj.album.network.networkframe.bean.BaseResp;
import com.azkj.album.network.networkframe.net.exception.ApiException;
import com.azkj.album.network.utils.ToastUtils;
import com.azkj.album.utils.PermissionsUtils;
import com.azkj.album.view.base.BaseActivity;
import com.azkj.album.view.fragment.GalleryFragmentSelect;
import com.azkj.album.view.fragment.gallery.GalleryChildFragmentSelect;
import com.azkj.album.view.widgets.dialog.DialogHelper;
import com.vachel.editor.PictureEditActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class GallerySelActivity extends BaseActivity {
    @BindView(R.id.upload)
    TextView upLoad;

    private GalleryBean mGalleryBean;

    private PermissionsUtils permissionsUtils = new PermissionsUtils();
    private String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

    @Override
    public void initView() {
        GalleryFragmentSelect galleryFragment = new GalleryFragmentSelect();
        galleryFragment.setListener(new GalleryChildFragmentSelect.OnGallerySelListener() {
            @Override
            public void onSel(GalleryBean bean, int position, boolean isShow) {
                mGalleryBean = bean;
            }

            @Override
            public void onShow(int position) {
//                if (position == 1) {
//                    upLoad.setVisibility(View.VISIBLE);
//                } else {
//                    upLoad.setVisibility(View.GONE);
//                }
            }
        });
        getSupportFragmentManager().beginTransaction().add(R.id.content, galleryFragment).commit();
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_gallery_sel;
    }

    @OnClick({R.id.cancel, R.id.confirm, R.id.upload})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.upload:
                permissionsUtils.checkPermissions(this, permissions, new PermissionsUtils.IPermissionsResult() {
                    @Override
                    public void passPermissions() {
                        Intent intent = new Intent(Intent.ACTION_PICK);
                        intent.setType("image/*");
                        startActivityForResult(intent, 101);
                    }

                    @Override
                    public void forbidPermissions() {
                        ToastUtils.showCenterToast("请打开存储权限");
                    }
                });
                break;
            case R.id.confirm:
                if (mGalleryBean != null) {
                    Intent intent = new Intent();
                    intent.putExtra("result", mGalleryBean);
                    setResult(100, intent);
                    finish();
                } else {
                    ToastUtils.showCenterToast("请选择一张贴图");
                }

                break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "requestCode:" + requestCode + "---resultCode:" + resultCode);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 101) {     //相册返回
                Uri uri = data.getData();
                Log.d(TAG, "album:" + uri.toString());
                Intent editIntent = new Intent(this, PictureEditActivity.class);
                editIntent.putExtra(PictureEditActivity.EXTRA_IMAGE_URI, uri);
                startActivityForResult(editIntent, 102);
            } else if (requestCode == 102) {     //编辑返回
                String path = data.getStringExtra(PictureEditActivity.RESULT_IMAGE_SAVE_PATH);
                Log.d(TAG, "album:" + path);
                new Handler().postDelayed(() -> NetworkMaster.getInstance().getCommonService().uploadPath(path, new UploadCallback<BaseResp<UploadBean>>() {
                    @Override
                    public void onSuccess(BaseResp<UploadBean> jsonObj) {
                        if (jsonObj.getCode() == 1) {
                            String url = jsonObj.getData().getFullurl();
                            EventBus.getDefault().post(new MessageEvent(MessageType.TYPE_ON_ACTIVITY_UPLOAD_MAP_SUCCESS, url, 1));
                        } else {
                            DialogHelper.hideDialog();
                        }
                    }

                    @Override
                    public void onError(ApiException apiException) {
                        DialogHelper.hideDialog();

                    }

                    @Override
                    public void onProgress(long currentLength, long contentLength) {

                    }
                }), 500);
            }
        }
    }
}
